import AsyncStorage from '@react-native-async-storage/async-storage';
const CAPTCHA_STORE_STORE_VALUES = 'CaptchaStore.values';
class CaptchaStore {
  constructor() {
    this.values = [];
  }

  async addValue(value) {
    if (!this.values.includes(value)) {
      this.values.push(value);
      try {
        let value1 = JSON.stringify(this.values);
        await AsyncStorage.setItem(CAPTCHA_STORE_STORE_VALUES, value1);
      } catch (e) {
        console.error(e);
      }
    }
    return this.values;
  }

  async getValues() {
    if (this.values.length <= 0) {
      this.values = JSON.parse(
        (await AsyncStorage.getItem(CAPTCHA_STORE_STORE_VALUES)) || '[]' || [],
      );
    }
    return this.values;
  }

  async delValue(value) {
    if (!value) {
      return this.values;
    }
    this.values = this.values.filter(v => v !== value);
    await AsyncStorage.setItem(
      CAPTCHA_STORE_STORE_VALUES,
      JSON.stringify(this.values),
    );
    return this.values;
  }
}
export default new CaptchaStore();
